<?php
namespace App\Http\Controllers\ApiWx;

use App\DBs\ThingDB;
use Illuminate\Http\Request;

class ThingController extends BaseController
{
    //物品

    protected $db;

    public function __construct()
    {
        parent::__construct();
        $this->db = new ThingDB();
    }

    /**
     * 码新增
     */
    public function setCodeAdd(Request $request)
    {
        for ($i=0;$i<100;$i++) {
            $str = $this->getStr();
            $resDb = $this->db->setCodeAdd($str);
            if ($resDb['code']!=0) {
                return $this->toWxJson(-500);
            }
        }
        return $this->toWxJson(0);
    }

    /**
     * 获取编号
     */
    public function getStr()
    {
        $rand = rand(6,12);
        $str = $this->db->getRandStr($rand);
        $resOne = $this->db->getCodeOne($str);
        if ($resOne['code']!=0) { return $str; }
        return $this->getStr();
    }

    /**
     * 品类列表
     */
    public function getCateIndex(Request $request)
    {
        $resDb = $this->db->getCateIndex();
        if ($resDb['code']!=0) {
            return $this->toWxJson(-3);
        }
        $items = $resDb['data'];
        return $this->toWxJson(0,$items);
    }

    /**
     * 品类新增
     */
    public function setCateAdd(Request $request)
    {
        $wx = $request->post('wx','');
        $name = $request->post('name','');
        $resDb = $this->db->getCateOne($name);
        if ($resDb['code']==0) {
            return $this->toWxJson(-5);
        }
        $resDb2 = $this->db->setCateAdd($wx,$name);
        if ($resDb2['code']!=0) {
            return $this->toWxJson(-500);
        }
        return $this->toWxJson(0);
    }

    /**
     * 物品一个
     */
    public function getOneByCode(Request $request)
    {
        $wx = $request->post('wx','');
        $code = $request->post('code','');
        $resDb = $this->db->getOneByCode($code);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-3);
        }
        $items = $resDb['data'];
        return $this->toWxJson(0,$items);
    }

    /**
     * 物品新增
     */
    public function setAdd(Request $request)
    {
        $wx = $request->post('wx','');
        $userType = $request->post('userType',1); //1果农2商家3用户
        $code = $request->post('code','');
        $cover = $request->post('cover','');
        $link = $request->post('link','');
        $name = $request->post('name','');
        $cate = $request->post('cateId',0);
        $year = $request->post('year','');
        $out = $request->post('out','');
        if (!$wx || !$code || !$cover || !$link || !$name) {
            return $this->toWxJson(-2);
        }
        if ($userType==1) {
            $year = $year * 365; $out = $out * 1000; $cate = 1;
        }
        $resDb = $this->db->setAdd($wx,$userType,$code,$cover,$link,$name,$cate,$year,$out);
        if ($resDb['code']!=0) {
            return $this->toWxJson(-500);
            //return array('code'=>'-500','msg'=>$resDb['msg']);
        }
        return $this->toWxJson(0);
    }
}